package com.antg.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 数据解析工具类
 * */
public class ParseUtil {
    //获取指定字段的数据
    public static String getUserData(String line,String field){
        String regex = field+"=(.*?),";
        Pattern pattern= Pattern.compile(regex);
        Matcher matcher = pattern.matcher(line);
        matcher.find();
        return matcher.group(1);
    }

    //一次性获取多个字段的值
    public static String[] getUserData(String line,String ...fields){
        String[] res = new String[fields.length];
        for(int i = 0;i<fields.length;i++){
            res[i] = getUserData(line,fields[i]);
        }
        return res;
    }

    //获取指定标签下的数据
    public static String getContentData(String line,String tag){
        String regex = "<"+tag+">"+"(.*?)"+"</"+tag+">";
        Pattern pattern= Pattern.compile(regex);
        Matcher matcher = pattern.matcher(line);
        matcher.find();
        return matcher.group(1);
    }

    //一次性获取多个标签的值
    public static String[] getContentData(String line,String ...tags){
        String[] res = new String[tags.length];
        for(int i = 0;i<tags.length;i++){
            res[i] = getContentData(line,tags[i]);
        }
        return res;
    }
}
